using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.Common;

namespace MarketOnlineDAO
{

    public class BaseDataProvider
    {
        public BaseDataProvider()
        {
            ConfigDataProvider.getConfigDataProvider();
        }
        
        public DataTable ExecuteSelectCommand(DbCommand command)
        {
            DataTable table = null;
            try
            {
                command.Connection.Open();
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Connection.Close();
            }
            return table;
        }

        public bool ExecuteNonQuery(DbCommand command)
        {
            bool rs = false;
            try
            {
                command.Connection.Open();
                rs = command.ExecuteNonQuery() != -1;
            }
            catch (Exception)
            {
                
            }
            finally
            {
                command.Connection.Close();
            }
            return rs;
        }

        public string ExecuteScalar(DbCommand command)
        {
            string value = "";
            try
            {
                command.Connection.Open();
                value = command.ExecuteScalar().ToString();
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                command.Connection.Close();
            }
            return value;
        }
        
        public DbCommand CreateCommand()
        {
            DbCommand comm = null;
            try
            {
                string dataProviderName = ConfigDataProvider.DbProviderName;
                string connectionString = ConfigDataProvider.DbConnectionString;
                DbProviderFactory factory;
                if (dataProviderName == "mysql")
                {
                    factory = new MySqlClientFactory();
                }
                else
                {
                    factory = DbProviderFactories.GetFactory(dataProviderName);
                }

                DbConnection conn = factory.CreateConnection();

                conn.ConnectionString = connectionString;
                comm = conn.CreateCommand();
                comm.CommandType = CommandType.StoredProcedure;
            }
            catch (Exception)
            {
                return null;            
            }
            return comm;
        }
        
        public MySqlCommand CreateMysqlCommand()
        {
            MySqlCommand comm = null;
            try
            {
                string dataProviderName = ConfigDataProvider.DbProviderName;
                string connectionString = ConfigDataProvider.DbConnectionString;
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = connectionString;
                comm = conn.CreateCommand();
                comm.CommandType = CommandType.StoredProcedure;
            }
            catch (Exception)
            {
                return null;      
            }
            return comm;
        }
        public MySqlCommand CreateMysqlCommand(CommandType type)
        {
            MySqlCommand comm = null;
            try
            {
                string dataProviderName = ConfigDataProvider.DbProviderName;
                string connectionString = ConfigDataProvider.DbConnectionString;
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = connectionString;
                comm = conn.CreateCommand();
                comm.CommandType = type;
            }
            catch (Exception)
            {
                return null;          
            }
            return comm;
        }

        public DbConnection CreateConnection()
        {
            string dataProviderName = ConfigDataProvider.DbProviderName;
            string connectionString = ConfigDataProvider.DbConnectionString;
            DbProviderFactory factory;
            if (dataProviderName == "mysql")
            {
                factory = new MySqlClientFactory();
            }
            else
            {
                factory = DbProviderFactories.GetFactory(dataProviderName);
            }
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            return conn;
        }
    }
}
